package resources;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import src.Window;

/**
 * 
 * classe responsavel por manter o arquivo do codigo
 * tem metodos para salvar, abrir e salvar como
 * tambem uma variavel booleana para dizer se o arquivo foi modificado ou nao
 * 
 * @author eklauberg
 *
 */
public class Document {
	
	protected static JFileChooser fileChooser;
	private static boolean modificado;
	protected static File file;
	protected static Window content;
	
	
	public static void setContent(Window window){
		Document.content = window;
	}
	
	public static void open() {
		if (isModificado()) {
			JOptionPane jOptionPane = new JOptionPane();
			int key = JOptionPane.showConfirmDialog(null,
					"Deseja salvar o arquivo?");
			switch (key) {
			case JOptionPane.YES_OPTION:
				saveas();
				_open();
				break;
			case JOptionPane.NO_OPTION:
				_open();
				break;
			case JOptionPane.CANCEL_OPTION:
				break;
			}
		}else{
			_open();
		}
	}
	protected static void _open(){
		fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		switch (fileChooser.showOpenDialog(null)) {
			case JFileChooser.APPROVE_OPTION:
				file = fileChooser.getSelectedFile();			
			break;
		}	
	}
	
	public static void saveas(){
		fileChooser = new JFileChooser();
		int key = fileChooser.showSaveDialog(null);
		if(key != JFileChooser.APPROVE_OPTION)
			return;
		
		File file = fileChooser.getSelectedFile();		
		try {
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content.getTxtCodigo().getText());
			bw.close();		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setModificado(false);
	}	
		
	public static void save() {
		if(file == null){
			saveas();
			return;
		}
		if(!file.exists()){
			saveas();
		}else{			
			try {
				FileWriter fw = new FileWriter(file.getAbsoluteFile());
				BufferedWriter bw = new BufferedWriter(fw);
				bw.write(content.getTxtCodigo().getText());
				bw.close();				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}			
	}
		
	public static boolean isModificado() {
		return modificado;
	}
	
	public static void setModificado(boolean modificado) {
		Document.modificado = modificado;
	}	
	
	public static String getNewFileContent(){
		try {
			return readFile(file.getAbsolutePath(),Charset.defaultCharset());
		} catch (IOException e) {
			e.printStackTrace();
			return "erro ao ler o arquivo";
		}		
	}
	
	public static String readFile(String path, Charset encoding) throws IOException {
		byte[] encoded = Files.readAllBytes(Paths.get(path));
		return new String(encoded, encoding);
	}	
}
